from typing import Dict
class Solution:
    def __init__(self):
        self.stored_list:Dict = {}

    def get_list_by_end_number(self, end_number):
        if end_number in self.stored_list.keys():
            return self.stored_list[end_number]
        else:
            new_list = [0]
            # i = 1
            tmp = 1
            while True:
                tmp*=end_number
                tmp = tmp%10
                if tmp in new_list:
                    break
                new_list.append(tmp)
            new_list[0] = 1
            self.stored_list[end_number%10] = new_list
            return new_list

    def get_end_tail_number(self,init_end_number, num):
        new_list = self.get_list_by_end_number(init_end_number)
        new_num = num%(len(new_list)-1)
        return new_list[new_num]
        

if __name__ == '__main__':
    s = Solution()
    # for i in range(1,10):
    #     print(s.get_list_by_end_number(i))
    out = []
    while True:
        inputstr = input()
        init_number, num = inputstr.split(" ")
        if init_number == "0" and num == "0":
            break
        # print(f"{init_number}, {num}")
        tail = s.get_end_tail_number(int(init_number)%10, int(num))
        out.append(tail)
    print("\n".join(map(str, out)))